#coding=utf-8

__doc__= '''

     General logging module

'''
#changes location here

import sys,os
import time, EncodingConvert as ec


LOGGING_FILE = 'log.txt'


LVL_INFO = 0
LVL_DEBUG = 1
LVL_ERROR = 2
SILENT = False

def log(module=None,msg='',level=LVL_INFO,stdout=True):
    ttime = time.asctime()
    if not module:
        module = __name__
    f = None
    log_str = ''
    if SILENT == False:
        #print LOGGING_FILE
        msg = ec.zh2gbk(msg)[1]
        log_str = '[%s] " %s " in module <%s> at %s \n' % (level,msg,module,ttime)
        if level >= LVL_INFO:
                f = open(LOGGING_FILE,'a')
                try:
                    f.write(log_str)
                except :
                    f.write('Exception in writing logfile')
                f.close()
    if stdout:
        try:
            print msg    
        except Exception,ext:
            print Exception,ext
        
    
    
    